package cc.mrbird.febs.accounting.service.template;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TemplateStrategyFactory implements ApplicationContextAware {
    private static final Map<Integer, TemplateStrategy> STRATEGY_MAP = new HashMap<>();

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // 获取所有实现了 ImportStrategy 接口的 bean
        Map<String, TemplateStrategy> beans = applicationContext.getBeansOfType(TemplateStrategy.class);
        for (TemplateStrategy strategy : beans.values()) {
            TemplateType annotation = strategy.getClass().getAnnotation(TemplateType.class);
            if (annotation != null) {
                int type = annotation.value();
                STRATEGY_MAP.put(type, strategy);
            }
        }
    }

    public static TemplateStrategy getStrategy(int type) {
        return STRATEGY_MAP.get(type);
    }
}
